﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemList : MonoBehaviour 
{
    public SceneObjectCreator Creator;
    public SceneItemUI ItemTemplete;
    public SceneItemUI currentSelectItemUI;
    public GameObject ItemRoot;
    public UnityEngine.UI.Button CloseButton;
    public UnityEngine.UI.Button PutButton;

    // Use this for initialization

    void Awake()
    {

    }

	void Start ()
    {
        CloseButton.onClick.AddListener(Close);
        PutButton.onClick.AddListener(PutItem);
        for(int i = 0 ; i < Creator.SceneObjects.Length;i++)
        {
            SceneItemUI theui = GameObject.Instantiate(ItemTemplete, ItemRoot.transform);
            theui.gameObject.SetActive(true);
            theui.sceneObj = Creator.SceneObjects[i];
            theui.SelectitemEvent += () =>
                {
                    if (currentSelectItemUI != null)
                    {
                        currentSelectItemUI.SetSelect(false);
                    }
                    currentSelectItemUI = theui;
                    currentSelectItemUI.SetSelect(true);
                };
        };
	}
	
	// Update is called once per frame
	void Update ()
    {
		
	}

    public void Open()
    {
        gameObject.SetActive(true);

    }

    public void Close()
    {
        gameObject.SetActive(false);
    }

    public void PutItem()
    {
        if(currentSelectItemUI != null)
        {
            SceneObject so = GameObject.Instantiate(currentSelectItemUI.sceneObj);
            so.SetPosition(GridInfo.Instance.CurrentSelectGrid.X, GridInfo.Instance.CurrentSelectGrid.Y);
        }
    }

}
